package util.webSearch;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

import puppy.demo.tagRanking.TagRankerSimple;

import util.io.FileInput;

public class QSuggestionsAggretator {

	public static void ini(String path) throws UnsupportedEncodingException {

		FileInput in = new FileInput(path);

		String pathSuffix = "/home/sergio/Dropbox/data/delicious/";
		 String AGENT_LIST ="/home/sergio/projects/dictionary/user_agent_list.txt";
		// String output = pathSuffix + "model_filtered.bin";

		String output = pathSuffix + "model_filtered_K1.bin";
		 TagRankerSimple tagRanker = new TagRankerSimple(output);

		String line = in.readString();

		while (line != null) {

			System.out.println("Information query:" + line);
			HashSet<String> set = new HashSet<String>();
			LinkedList<String> list = GoogleQuery.getRelatedSearches(line,AGENT_LIST,10);
			printSuggestionsList(line, list, Constants.GOOGLE, set);

			list = YahooQuery.getRelatedSearches(line);

			printSuggestionsList(line, list, Constants.YAHOO, set);

			
			 // sergio's method without user weight list =
			  list = tagRanker.getSuggestions(line, false); 
			  printSuggestionsList(line, list, Constants.SERGIO,set); 
			  
			  // sergio's method with user weight list =
			  list =tagRanker.getSuggestions(line, true); 
			  printSuggestionsList(line, list, Constants.SERGIO_USERINFO,set); 
			 

			// print tags to be evaluated (without repetions

			printRandomSet(set);
			System.out.println("");

			line = in.readString();

		}
	}
	
	private static  void printRandomSet(HashSet<String> set) {
		Iterator<String> iter = set.iterator();
		LinkedList<String> list = new LinkedList<String>();

		while (iter.hasNext()) {
			list.add(iter.next());

		}

		while (list.size() > 0) {
			int j = (int) (Math.random() * list.size());

			String element = list.remove(j);

			System.out.print(element + "\t" + element + "\t");
		}
	
System.out.println("");
	}

	private static void printSuggestionsList(String query,
			LinkedList<String> list, int method, HashSet<String> sugges) {
		// TODO Auto-generated method stub
		int j = 1;
		for (int i = 0; i < list.size(); i++) {

			String suggestion = list.get(i);

			suggestion = suggestion.replace("&#39;s", "");
			suggestion = suggestion.replace("&#39;", "");
			suggestion = suggestion.replace(query, "");

			if (!suggestion.equals("")) {
				sugges.add(suggestion.trim());
				System.out.println(query + "\t" + suggestion + "\t" + (j)
						+ "\t" + method);
				j++;

			}

		}
		
		

	}

	/**
	 * @param args
	 * @throws UnsupportedEncodingException
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub

		String queryPath = "/home/sergio/Dropbox/data/queryList2.txt";
		ini(queryPath);
	
	
		

	}

}
